﻿#region Copyright

//--------------------------------------------------------------------------------------------------------
// <copyright file="CommentBO.cs" company="DNN Corp®">
//      DNN Corp® - http://www.dnnsoftware.com Copyright (c) 2002-2013 by DNN Corp®
//
//      Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
//      associated documentation files (the "Software"), to deal in the Software without restriction,
//      including without limitation the rights to use, copy, modify, merge, publish, distribute,
//      sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
//      furnished to do so, subject to the following conditions:
//
//      The above copyright notice and this permission notice shall be included in all copies or
//      substantial portions of the Software.
//
//      THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
//      NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//      NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//      DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//      OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
////------------------------------------------------------------------------------------------------------

#endregion Copyright

using DotNetNuke.Wiki.BusinessObjects.Models;
using DotNetNuke.Wiki.Utilities;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace DotNetNuke.Wiki.BusinessObjects
{
    /// <summary>
    /// Comment Business Object based on the _AbstractBusinessObject
    /// </summary>
    public class CommentBO : _AbstractBusinessObject<Comment, int>
    {
        #region Variables

        private UnitOfWork currentUnitOfWork;

        #endregion Variables

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="CommentBO"/> class.
        /// </summary>
        /// <param name="uow">The UNit of Work.</param>
        public CommentBO(UnitOfWork uow)
            : base(uow.Context)
        {
            this.currentUnitOfWork = uow;
        }

        #endregion Constructor

        #region Enums

        /// <summary>
        /// The possible controlled errors generated by this class
        /// </summary>
        public enum CommentError
        {
            /// <summary>
            /// The error1
            /// </summary>
            Error1 = 1,

            /// <summary>
            /// The error2
            /// </summary>
            Error2 = 2,

            /// <summary>
            /// The error3
            /// </summary>
            Error3 = 3
        }

        #endregion Enums

        #region Methods

        /// <summary>
        /// Deletes all the comments with the parent id
        /// </summary>
        /// <param name="parentId">the parent id</param>
        internal void DeleteComments(int parentId)
        {
            this.DatabaseContext.Execute(CommandType.Text, string.Concat("DELETE FROM ", this.DataBaseOwner, this.ObjectQualifier, "Wiki_Comment WHERE ParentId=@0"), parentId);
        }

        /// <summary>
        /// Entity_s the evaluate SQL exception.
        /// </summary>
        /// <param name="exc">The exception intercepted.</param>
        /// <param name="crudOperation">The crud operation.</param>
        /// <exception cref="System.NotImplementedException">This is the exception
        /// thrown</exception>
        internal override void Entity_EvaluateSqlException(
            SqlException exc,
            SharedEnum.CrudOperation crudOperation)
        {
            throw new System.NotImplementedException();
        }

        /// <summary>
        /// Gets all comments associated to the parent id passed has parameter
        /// </summary>
        /// <param name="parentid">the parent id of the comments</param>
        /// <returns>returns collection of comments</returns>
        internal IEnumerable<Comment> GetCommentsByParent(int parentid)
        {
            return this.DatabaseContext.ExecuteQuery<Comment>(CommandType.Text, string.Concat("SELECT * FROM ", this.DataBaseOwner, this.ObjectQualifier, "Wiki_Comments where ParentId=@0 order by Datetime desc"), parentid);
        }

        /// <summary>
        /// Gets the number of comments associated to the Parent ID passed
        /// </summary>
        /// <param name="parentId">The parent unique identifier.</param>
        /// <returns>Returns a integer value representing the count</returns>
        internal int GetCommentCount(int parentId)
        {
            return this.DatabaseContext.ExecuteScalar<int>(CommandType.Text, string.Concat("SELECT Count(CommentId) FROM ", this.DataBaseOwner, this.ObjectQualifier, "Wiki_Comments where ParentId=@0"), parentId);
        }

        /// <summary>
        /// Gets the comment notify users.
        /// </summary>
        /// <param name="parentId">The parent unique identifier.</param>
        /// <returns>Email Address</returns>
        internal IEnumerable<CommentEmails> GetCommentNotifyUsers(int parentId)
        {
            return this.DatabaseContext.ExecuteQuery<CommentEmails>(CommandType.Text, string.Concat("select DISTINCT(Email) from ", this.DataBaseOwner, this.ObjectQualifier, "Wiki_Comments where ParentId=@0 AND EmailNotify = 1"), parentId);
        }

        #endregion Methods
    }

    /// <summary>
    /// Comment Emails Class
    /// </summary>
    internal class CommentEmails
    {
        #region "Variables"

        private string mEmailValue;

        #endregion "Variables"

        #region "Properties"

        /// <summary>
        /// Gets or sets the email.
        /// </summary>
        /// <value>The email.</value>
        public string Email
        {
            get { return this.mEmailValue; }
            set { this.mEmailValue = value; }
        }

        #endregion "Properties"

        #region "Methods"

        /// <summary>
        /// Initializes a new instance of the <see cref="CommentEmails"/> class.
        /// </summary>
        public CommentEmails()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CommentEmails"/> class.
        /// </summary>
        /// <param name="email">The email.</param>
        public CommentEmails(string email)
        {
            this.Email = email;
        }

        #endregion "Methods"
    }
}